package org.example.carsystem.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.util.StringUtil;
import org.example.carsystem.domain.SysDictData;
import org.example.carsystem.domain.SysDictType;
import org.example.carsystem.domain.vo.DictTypeVo;
import org.example.carsystem.service.DictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/dict/type")
@CrossOrigin
public class DictTypeController {
    @Autowired
    private DictTypeService dictTypeService;

    @GetMapping("/select")
    public Map<String, Object> dictTypeSelect(@RequestParam(value = "dictName",required = false)String dictName,
                                              @RequestParam(value = "dictType",required = false)String dictType,
                                              @RequestParam(value = "status",required = false)String status,
                                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date createStartTime,
                                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date createEndTime,
                                              @RequestParam("pageNum")Integer pageNum, @RequestParam("pageSize")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<SysDictType> list = dictTypeService.dictTypeSelect(dictName,dictType,status,createStartTime,createEndTime);


        if(!StrUtil.isEmpty(dictName)){
            list.forEach(i->{
                String s=i.getDictName().replaceAll(dictName,"<font color='red'>"+dictName+"</font>");
                i.setDictName(s);
            });

        }
        PageInfo<SysDictType> info = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("list",info);
        return map;
    }

    @GetMapping("/select/all")
    public Map<String,Object> dictTypeSelectAll(){
        List<DictTypeVo> list = dictTypeService.dictTypeSelectAll();
        Map<String, Object> map = new HashMap<>();
        map.put("list",list);
        return map;
    }

    //字典类型表
    @RequestMapping("/typelist")
    public PageInfo<SysDictType> list(@RequestBody SysDictType type, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<SysDictType> list=dictTypeService.listAll(type);

        PageInfo<SysDictType> info = new PageInfo<>(list);

        return  info;

    }
    @RequestMapping("removetype/{dictType}")
    public Integer removetype(@PathVariable String dictType){
        Integer removetype=dictTypeService.removetype(dictType);
        return removetype;

    }

    @RequestMapping("tlist")
    public List<SysDictType> tlist(){
        List<SysDictType> types = dictTypeService.list();
        return types;

    }
    @RequestMapping("/typelists")
    public List<SysDictType> lists(SysDictType type){
        List<SysDictType> list=dictTypeService.listAll(type);
        return  list;

    }



    //查询字典类型详细
    @RequestMapping("dictId/{dictId}")
    public SysDictType getInfo(@PathVariable Long dictId){
        SysDictType tt=dictTypeService.selectById(dictId);
        return tt;

    }

    //新增字典类型
    @RequestMapping("addtype")
    public Integer addtype(@RequestBody SysDictType type){
        type.setCreateBy("xyr");
        type.setCreateTime(new DateTime());
        Integer addtype = dictTypeService.addtype(type);


        return addtype;
    }



    @RequestMapping("updatetype")
    public Integer updatetype(@RequestBody SysDictType type){
        type.setCreateBy("xyr");
        type.setCreateTime(new DateTime());
        Integer updatetype=dictTypeService.updatetype(type);
        return updatetype;
    }

}
